/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 *
 * HDF is dual licensed: you can use it either under the terms of
 * the GPL, or the BSD license, at your option.
 * See the LICENSE file in the root of this repository for complete details.
 */

#include "accel_sc7a20e.h"
#include <securec.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "sensor_accel_driver.h"
#include "sensor_config_controller.h"
#include "sensor_device_manager.h"

#define HDF_LOG_TAG    hdf_sensor_accel

static struct Sc7a20eDrvData *g_sc7a20eDrvData = NULL;

struct Sc7a20eDrvData *Sc7a20eGetDrvData(void)
{
    return g_sc7a20eDrvData;
}

static int32_t ReadSc7a20eRawData(struct SensorCfgData *data, struct AccelData *rawData, uint64_t *timestamp)
{
    uint8_t reg[ACCEL_AXIS_BUTT];
    OsalTimespec time;

    int32_t ret;
    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
    (void)memset_s(reg, sizeof(reg), 0, sizeof(reg));

    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);

    if (OsalGetTime(&time) != HDF_SUCCESS) {
        HDF_LOGD("%s: Get time failed", __func__);
        return HDF_FAILURE;
    }
    *timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */

    ret = ReadSensor(&data->busCfg, SC7A20E_ACCEL_X_LSB_ADDR, &reg[ACCEL_X_AXIS_LSB], sizeof(uint8_t));
    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");

    ret = ReadSensor(&data->busCfg, SC7A20E_ACCEL_X_MSB_ADDR, &reg[ACCEL_X_AXIS_MSB], sizeof(uint8_t));
    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");

    ret = ReadSensor(&data->busCfg, SC7A20E_ACCEL_Y_LSB_ADDR, &reg[ACCEL_Y_AXIS_LSB], sizeof(uint8_t));
    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");

    ret = ReadSensor(&data->busCfg, SC7A20E_ACCEL_Y_MSB_ADDR, &reg[ACCEL_Y_AXIS_MSB], sizeof(uint8_t));
    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");

    ret = ReadSensor(&data->busCfg, SC7A20E_ACCEL_Z_LSB_ADDR, &reg[ACCEL_Z_AXIS_LSB], sizeof(uint8_t));
    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");

    ret = ReadSensor(&data->busCfg, SC7A20E_ACCEL_Z_MSB_ADDR, &reg[ACCEL_Z_AXIS_MSB], sizeof(uint8_t));
    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");

    rawData->x = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ACCEL_X_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
        reg[ACCEL_X_AXIS_LSB]);
    rawData->y = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ACCEL_Y_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
        reg[ACCEL_Y_AXIS_LSB]);
    rawData->z = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ACCEL_Z_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
        reg[ACCEL_Z_AXIS_LSB]);
    
    HDF_LOGD("%s:ACCEL_SC7A20E rawData is : %d, %d, %d", __func__, rawData->x, rawData->y, rawData->z);

    return HDF_SUCCESS;
}

int32_t ReadSc7a20eData(struct SensorCfgData *cfg, struct SensorReportEvent *event)
{
    int32_t ret;
    struct AccelData rawData = { 0, 0, 0 };
    static int32_t tmp[ACCEL_AXIS_NUM];

    CHECK_NULL_PTR_RETURN_VALUE(cfg, HDF_ERR_INVALID_PARAM);
    CHECK_NULL_PTR_RETURN_VALUE(event, HDF_ERR_INVALID_PARAM);

    ret = ReadSc7a20eRawData(cfg, &rawData, &event->timestamp);
    if (ret != HDF_SUCCESS) {
        HDF_LOGD("%s: SC7A20E read raw data failed", __func__);
        return HDF_FAILURE;
    }

    event->sensorId = SENSOR_TAG_ACCELEROMETER;
    event->option = 0;
    event->mode = SENSOR_WORK_MODE_REALTIME;

    tmp[ACCEL_X_AXIS] = -(rawData.z * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
    tmp[ACCEL_Y_AXIS] = (rawData.x * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
    tmp[ACCEL_Z_AXIS] = (rawData.y * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;

    event->dataLen = sizeof(tmp);
    event->data = (uint8_t *)&tmp;

    HDF_LOGD("%s: SC7A20E data x %d, y %d, z %d", __func__,tmp[ACCEL_X_AXIS],tmp[ACCEL_Y_AXIS],tmp[ACCEL_Z_AXIS]);
    return ret;
}

static int32_t InitSc7a20e(struct SensorCfgData *data)
{
    int32_t ret;

    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);

    ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
    if (ret != HDF_SUCCESS) {
        HDF_LOGD("%s: SC7A20E sensor init config failed", __func__);
        return HDF_FAILURE;
    }
    return HDF_SUCCESS;
}

static int32_t DispatchSC7A20E(struct HdfDeviceIoClient *client,
    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
    (void)client;
    (void)cmd;
    (void)data;
    (void)reply;

    return HDF_SUCCESS;
}

int32_t Sc7a20eBindDriver(struct HdfDeviceObject *device)
{
    struct Sc7a20eDrvData *drvData = NULL;

    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);

    drvData = (struct Sc7a20eDrvData *)OsalMemCalloc(sizeof(*drvData));
    if (drvData == NULL) {
        HDF_LOGD("%s: Malloc SC7A20E drv data fail", __func__);
        return HDF_ERR_MALLOC_FAIL;
    }

    drvData->ioService.Dispatch = DispatchSC7A20E;
    drvData->device = device;
    device->service = &drvData->ioService;
    g_sc7a20eDrvData = drvData;

    return HDF_SUCCESS;
}

int32_t Sc7a20eInitDriver(struct HdfDeviceObject *device)
{
    int32_t ret;
    struct AccelOpsCall ops;
    struct Sc7a20eDrvData *drvData = NULL;

    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
    drvData = (struct Sc7a20eDrvData *)device->service;
    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);


    drvData->sensorCfg = AccelCreateCfgData(device->property);
    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
        HDF_LOGD("%s: Creating accelcfg failed because detection failed", __func__);
        return HDF_ERR_NOT_SUPPORT;
    }

    ops.Init = NULL;
    ops.ReadData = ReadSc7a20eData;
    ret = AccelRegisterChipOps(&ops);
    if (ret != HDF_SUCCESS) {
        HDF_LOGD("%s: Register SC7A20E accel failed", __func__);
        return HDF_FAILURE;
    }

    ret = InitSc7a20e(drvData->sensorCfg);
    if (ret != HDF_SUCCESS) {
        HDF_LOGD("%s: Init SC7A20E accel failed", __func__);
        return HDF_FAILURE;
    }

    return HDF_SUCCESS;
}

void Sc7a20eReleaseDriver(struct HdfDeviceObject *device)
{
    struct Sc7a20eDrvData *drvData = NULL;

    CHECK_NULL_PTR_RETURN(device);
    drvData = (struct Sc7a20eDrvData *)device->service;
    CHECK_NULL_PTR_RETURN(drvData);

    AccelReleaseCfgData(drvData->sensorCfg);
    drvData->sensorCfg = NULL;
    OsalMemFree(drvData);
}

struct HdfDriverEntry g_accelSc7a20eDevEntry = {
    .moduleVersion = 1,
    .moduleName = "HDF_SENSOR_ACCEL_SC7A20E",
    .Bind = Sc7a20eBindDriver,
    .Init = Sc7a20eInitDriver,
    .Release = Sc7a20eReleaseDriver,
};

HDF_INIT(g_accelSc7a20eDevEntry);